package com.bac.bacplatform;

import java.util.HashMap;
import java.util.Map;

import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.bac.http.VolleyErrorHelper;
import com.bac.utils.Config;
import com.bac.utils.Util;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class ScoreDialog extends Dialog implements android.view.View.OnClickListener,ErrorListener{
	private ImageView mImageView1,
					  mImageView2,
					  mImageView3,
					  mImageView4,
					  mImageView5;

	Context context;
	
	private int score = 0;
	
	private Button btn_score_ok;
	private Button btn_score_cancle;

	public ScoreDialog(Context context, int theme) {
		super(context,theme);
		this.context = context;
	}
	public ScoreDialog(Context context){
		super(context);
		this.context = context;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.scoredialog);
		init();
	}
	public void  init(){
		mImageView1 = (ImageView) findViewById(R.id.score_image1);
		mImageView1.setOnClickListener(this);
		mImageView2 = (ImageView) findViewById(R.id.score_image2);
		mImageView2.setOnClickListener(this);
		mImageView3 = (ImageView) findViewById(R.id.score_image3);
		mImageView3.setOnClickListener(this);
		mImageView4 = (ImageView) findViewById(R.id.score_image4);
		mImageView4.setOnClickListener(this);
		mImageView5 = (ImageView) findViewById(R.id.score_image5);
		mImageView5.setOnClickListener(this);
		btn_score_ok = (Button) findViewById(R.id.btn_score_ok);
		btn_score_ok.setOnClickListener(this);
		btn_score_cancle = (Button) findViewById(R.id.btn_score_cancle);
		btn_score_cancle.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.score_image1:
//			Toast.makeText(getContext(), "1分", Toast.LENGTH_SHORT).show();
			imageScoreChange(1);
			score = 1;
			break;
		case R.id.score_image2:
//			Toast.makeText(getContext(), "2分", Toast.LENGTH_SHORT).show();
			imageScoreChange(2);
			score = 2;
			break;
		case R.id.score_image3:
//			Toast.makeText(getContext(), "3分", Toast.LENGTH_SHORT).show();
			imageScoreChange(3);
			score = 3;
			break;
		case R.id.score_image4:
//			Toast.makeText(getContext(), "4分", Toast.LENGTH_SHORT).show();
			imageScoreChange(4);
			score = 4;
			break;
		case R.id.score_image5:
//			Toast.makeText(getContext(), "5分", Toast.LENGTH_SHORT).show();
			imageScoreChange(5);
			score = 5;
			break;
		case R.id.btn_score_ok:
			if(rate()){
				dismiss();
			}
			break;
		case R.id.btn_score_cancle:
			dismiss();
			break;

		default:
			break;
		}
	}
	
	public void imageScoreChange(int i){
		mImageView1.setImageDrawable(context.getResources().getDrawable(R.drawable.score_icon_rate_gray));
		mImageView2.setImageDrawable(context.getResources().getDrawable(R.drawable.score_icon_rate_gray));
		mImageView3.setImageDrawable(context.getResources().getDrawable(R.drawable.score_icon_rate_gray));
		mImageView4.setImageDrawable(context.getResources().getDrawable(R.drawable.score_icon_rate_gray));
		mImageView5.setImageDrawable(context.getResources().getDrawable(R.drawable.score_icon_rate_gray));
		
		if (i == 1) {
			mImageView1.setImageDrawable(context.getResources().getDrawable(R.drawable.score_icon_rate));
		}else if (i == 2) {              
			mImageView1.setImageDrawable(context.getResources().getDrawable(R.drawable.score_icon_rate));
			mImageView2.setImageDrawable(context.getResources().getDrawable(R.drawable.score_icon_rate));
		}else if (i == 3) {              
			mImageView1.setImageDrawable(context.getResources().getDrawable(R.drawable.score_icon_rate));
			mImageView2.setImageDrawable(context.getResources().getDrawable(R.drawable.score_icon_rate));
			mImageView3.setImageDrawable(context.getResources().getDrawable(R.drawable.score_icon_rate));
		}else if (i == 4) {              
			mImageView1.setImageDrawable(context.getResources().getDrawable(R.drawable.score_icon_rate));
			mImageView2.setImageDrawable(context.getResources().getDrawable(R.drawable.score_icon_rate));
			mImageView3.setImageDrawable(context.getResources().getDrawable(R.drawable.score_icon_rate));
			mImageView4.setImageDrawable(context.getResources().getDrawable(R.drawable.score_icon_rate));
		}else if (i == 5) {              
			mImageView1.setImageDrawable(context.getResources().getDrawable(R.drawable.score_icon_rate));
			mImageView2.setImageDrawable(context.getResources().getDrawable(R.drawable.score_icon_rate));
			mImageView3.setImageDrawable(context.getResources().getDrawable(R.drawable.score_icon_rate));
			mImageView4.setImageDrawable(context.getResources().getDrawable(R.drawable.score_icon_rate));
			mImageView5.setImageDrawable(context.getResources().getDrawable(R.drawable.score_icon_rate));
		}
	}
	
	/**
	 * 加油站评分
	 */
	public boolean rate(){
		/**
		 * 成功返回结果
		 */
		Listener<String> sL = new Response.Listener<String>() {
			@Override
			public void onResponse(String response) {
				Log.i("TAG----success", response.toString());
			}
		};
		
		if(score==0){
			return false;
		}

		String path = Config.URL_GAS_SCORE;
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", GasInformationActivity.location.getId()+"");
		map.put("rating", score+"");
		Util.httpStringPost(path, sL, (GasInformationActivity)this.context, map);
		return true;
	}
	
	@Override
	public void onErrorResponse(VolleyError error) {
	}
}
 